package org.scboy.dbms.valid.datatype;

import java.util.ArrayList;
import java.util.List;

import org.scboy.dbms.annotation.ValidType;
import org.scboy.dbms.entity.ParamConfig;
import org.scboy.dbms.entity.ParamType;
import org.scboy.dbms.utils.JavaScriptUtil;
import org.scboy.dbms.valid.Valid;
import org.scboy.utils.entitys.coobi.Model;

@ValidType("JS规则验证")
public class JSValid implements Valid {

	@Override
	public List<ParamConfig> config() {
		List<ParamConfig> config=new ArrayList<ParamConfig>();
		config.add(new ParamConfig("msg", "消息文字","提示消息的文字。"));
		config.add(new ParamConfig("js", "表达式",ParamType.textarea,"js表达式，返回true和false。参数为value和params。"));
		return config;
	}

	@Override
	public String toString(Model config) {
		return config.getString("msg");
	}

	@Override
	public boolean valid(Object value,Model model, Model config) {
		StringBuilder exp = new StringBuilder("function valid(value,params){");
		exp.append(config.getString("js"));
		exp.append("}");
		Object result=JavaScriptUtil.fomartReturnObj(exp.toString(),"valid",value,model);
		if(result==null) {
			return false;
		}else {
			return (boolean)result;
		}
	}
	
}
